<?php
// @formatter:off
/**
 * @file s.editcollection.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-16 23:56:49 +0000 (Sun, 16 Jun 2013) $
 *
 * $Id: s.editcollection.php 73 2013-06-16 23:56:49Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0.0.1/themes/default/controlers/simplecontainer/s.editcollection.php $
 */
// @formatter:on

if(defined('__SIMPLE_CONTAINER__') && $wcProfile->allowedTo(WC_PERM_COLLECTION_EDIT)) {
	global $wcGroupForbidden;
	global $wcCollectionsHolder;
	global $wcCollection;
	global $wcComic;
	global $wcThemeAssigns;

	if(count($wcCollectionsHolder->topItems($wcGroup)) > 0) {
		if($wcComic) {
			$wcThemeAssigns["ACTIONCONTENT"] = "/simplecontainer/s.editcollection.html";
			$wcThemeAssigns["EDIT-WHAT"] = "comic";
			$wcThemeAssigns["EDIT-ID"] = $wcComic->id;
			$wcThemeAssigns["EDIT-NAME"] = $wcComic->name;

			$collections = array();
			$currentCollection = $wcComic->collection ? $wcCollectionsHolder->item($wcComic->collection, $wcGroup) : false;
			$currentPath = array();

			if($currentCollection) {
				$currentPath = $currentCollection->collectionPath();
			} else {
				$currentPath = array(0 => array(
						"id" => -1,
						"name" => ""
					));
			}

			$parent = 0;
			foreach($currentPath as $level => $colInfo) {
				$items = false;
				if($parent) {
					$aux = $wcCollectionsHolder->item($parent, $wcGroup);
					if($aux && $aux->ok()) {
						$items = $aux->collections();
					}
				} else {
					$items = $wcCollectionsHolder->topItems($wcGroup);
				}

				uasort($items, "WCCollection::Sort");
				$selected = false;
				$list = array();
				foreach($items as $collection) {
					$aux = array(
						"id" => $collection->id,
						"name" => $collection->name,
					);
					if($currentCollection && $collection->id == $colInfo["id"]) {
						$aux["selected"] = true;
						$selected = true;
					}
					$list[] = $aux;
				}
				$collections[$level] = array(
					"level" => $level,
					"collections" => $list
				);
				if($selected) {
					$collections[$level]["none-selected"] = true;
				}

				$parent = $colInfo["id"];
			}

			$wcThemeAssigns['COLLECTION-SELECTORS'] = $collections;
			$wcThemeAssigns['COLLECTION-LEVEL'] = count($currentPath);
		} elseif($wcCollection) {
			$wcThemeAssigns["ACTIONCONTENT"] = "/simplecontainer/s.editcollection.html";
			$wcThemeAssigns["EDIT-WHAT"] = "collection";
			$wcThemeAssigns["EDIT-ID"] = $wcCollection->id;
			$wcThemeAssigns["EDIT-NAME"] = $wcCollection->name;

			$collections = array();
			$currentCollection = $wcCollection->parent ? $wcCollectionsHolder->item($wcCollection->parent, $wcGroup) : false;
			$currentPath = array();

			if($currentCollection) {
				$currentPath = $currentCollection->collectionPath();
			} else {
				$currentPath = array(0 => array(
						"id" => -1,
						"name" => ""
					));
			}

			$parent = 0;
			foreach($currentPath as $level => $colInfo) {
				$items = false;
				if($parent) {
					$aux = $wcCollectionsHolder->item($parent, $wcGroup);
					if($aux && $aux->ok()) {
						$items = $aux->collections();
					}
				} else {
					$items = $wcCollectionsHolder->topItems($wcGroup);
				}

				uasort($items, "WCCollection::Sort");
				$selected = false;
				$list = array();
				foreach($items as $collection) {
					if($collection->id == $wcCollection->id) {
						continue;
					}
					$aux = array(
						"id" => $collection->id,
						"name" => $collection->name,
					);
					if($currentCollection && $collection->id == $colInfo["id"]) {
						$aux["selected"] = true;
						$selected = true;
					}
					$list[] = $aux;
				}
				$collections[$level] = array(
					"level" => $level,
					"collections" => $list
				);
				if($selected) {
					$collections[$level]["none-selected"] = true;
				}

				$parent = $colInfo["id"];
			}

			$wcThemeAssigns['COLLECTION-SELECTORS'] = $collections;
			$wcThemeAssigns['COLLECTION-LEVEL'] = count($currentPath);
		} else {
			$wcThemeAssigns["error-string"] = "This item is not available for collection edition";
			require "{$this->_themeRoot}/controlers/contents/c.500.php";
		}
	} else {
		$wcThemeAssigns["error-message"] = "There are no top collections to select.";
		require "{$this->_themeRoot}/controlers/contents/c.500.php";
	}
} else {
	$wcThemeAssigns["error-string"] = "You're not allowed to set item's collection";
	require "{$this->_themeRoot}/controlers/contents/c.403.php";
}
?>